package cafe.adriel.androidaudiorecorder;

import cafe.adriel.androidaudiorecorder.model.AudioChannel;
import cafe.adriel.androidaudiorecorder.model.AudioSampleRate;
import cafe.adriel.androidaudiorecorder.model.AudioSource;
import ohos.agp.colors.RgbColor;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.media.audio.AudioStreamInfo;

/**
 * ex.
 *
 * @author lj
 * @since 2021-04-06
 */
public class Util {
    public Util() {
    }
    /**
     * wait method
     *
     * @param millis
     * @param callback
     * @param context
     */
    public static void wait(int millis, Runnable callback, Context context) {
        TaskDispatcher globalTaskDispatcher = context.getUITaskDispatcher();
        globalTaskDispatcher.delayDispatch(callback, millis);
    }

    /**
     * AudioSource 音频资源
     *
     * @param source
     * @param channel
     * @param sampleRate
     * @return AudioSource
     *
     * @noinspection checkstyle:JavadocMethod, checkstyle:JavadocMethod
     */
    public static com.example.omrecorder.AudioSource getMic(AudioSource source,
                                                            AudioChannel channel,
                                                            AudioSampleRate sampleRate) {
        return new com.example.omrecorder.AudioSource.Smart(
                source.getSource(),
                AudioStreamInfo.EncodingFormat.ENCODING_PCM_16BIT,
                channel.getChannel(),
                sampleRate.getSampleRate());
    }
    /**
     * 获取color颜色
     *
     * @param color
     * @return getDarkerColor
     */
    public static int getDarkerColor(int color) {
        float factor = 0.8f;
        RgbColor rgbColor = RgbColor.fromArgbInt(color);
        int a1 = Color.alpha(color);
        int r1 = rgbColor.getRed();
        int g1 = rgbColor.getGreen();
        int b1 = rgbColor.getBlue();
        return Color.argb(a1,
                Math.max((int) (r1 * factor), 0),
                Math.max((int) (g1 * factor), 0),
                Math.max((int) (b1 * factor), 0));
    }
    /**
     * 获取time
     *
     * @param seconds
     * @return string类型
     */
    public static String formatSeconds(int seconds) {
        return getTwoDecimalsValue(seconds / 3600) + ":"
                + getTwoDecimalsValue(seconds / 60) + ":"
                + getTwoDecimalsValue(seconds % 60);
    }

    private static String getTwoDecimalsValue(int value) {
        if (value >= 0 && value <= 9) {
            return "0" + value;
        } else {
            return value + "";
        }
    }
}